package com.example.jetpacktaobao.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.jetpacktaobao.R
import com.example.jetpacktaobao.databinding.ItemOnSellBinding
import com.example.jetpacktaobao.domain.DataX

class OnSellListAdapter : RecyclerView.Adapter<OnSellListAdapter.InnerHolder>() {

    private val mContentList = arrayListOf<DataX>()

    class InnerHolder(binding: ItemOnSellBinding) : RecyclerView.ViewHolder(binding.root) {
        val itemTitleTv = binding.itemTitleTv
        val ivIcon = binding.ivIcon
        val itemLinkTv = binding.itemLinkTv
        val itemIdTv = binding.itemIdTv
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
        val binding =
            ItemOnSellBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return InnerHolder(binding)
    }

    override fun getItemCount(): Int {
        return mContentList.size
    }

    override fun onBindViewHolder(holder: InnerHolder, position: Int) {
        holder.run {
            with(mContentList[position]) {
                itemTitleTv.text = title
                Glide.with(holder.itemView.context).load(iconUrl).into(ivIcon)
                itemLinkTv.text = "链接：$link"
                itemIdTv.text = "id：${id.toString()}"
            }
        }
    }

    fun setData(it: List<DataX>) {
        mContentList.clear()
        mContentList.addAll(it)
        notifyDataSetChanged()
    }
}